// pages/viewsons/new_address/new_address.js
import api from '../../http/api'
//全局变量
const app = getApp();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        refresh_loading:'',
        address_num:'',
    },
    //提交地址
    formSubmit(e){
        this.setData({
            refresh_loading:true
        })
        let address = {
            "username": e.detail.value.username,
            "phone": e.detail.value.phone,
            "province": e.detail.value.province,
            "city": e.detail.value.city,
            "town": e.detail.value.town,
            "detail_address": e.detail.value.detail_address,
        }
        //将信息插入数据
        if(address.username != null && address.username != undefined){
            api.postData('/address_lists/insert?username=' +
            address.username + '&sex='+address.sex+'&phone=' + address.phone + "&province=" + address.province + '&city=' + address.city + '&town='+address.town+'&detail_address=' + address.detail_address+'&num'+
            app.globalData.address_num
            ).then(res => {
            //处理返回结果
            console.log('插入成功返回数据', res);
            //跳回收货页面
            wx.navigateBack({
                data:1,
            })
            })
        }
       let timer = setInterval(() => {
            this.setData({
                refresh_loading:false
            })
            clearInterval(timer)
            if(address.username == null && address.username == undefined){
                wx.navigateBack({
                    data:1
                })
            }
        }, 1000);
    },
    cancel(){
        wx.navigateBack({
            data:1,
        })
    },
 
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
    
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
        api.getData('/address_lists').then(res=>{
            this.setData({
                address_num:res.params.data.length
            })
        })
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})